//
// File: mean.cpp
//
// MATLAB Coder version            : 5.4
// C/C++ source code generated on  : 21-Mar-2025 16:18:04
//

// Include Files
#include "mean.h"
#include "rt_nonfinite.h"
#include "coder_array.h"

// Function Definitions
//
// Arguments    : const ::coder::array<double, 2U> &x
//                ::coder::array<double, 1U> &y
// Return Type  : void
//
namespace coder {
void mean(const ::coder::array<double, 2U> &x, ::coder::array<double, 1U> &y)
{
  array<double, 1U> bsum;
  int firstBlockLength;
  int xblockoffset;
  if ((x.size(0) == 0) || (x.size(1) == 0)) {
    y.set_size(x.size(0));
    firstBlockLength = x.size(0);
    for (xblockoffset = 0; xblockoffset < firstBlockLength; xblockoffset++) {
      y[xblockoffset] = 0.0;
    }
  } else {
    int bvstride;
    int lastBlockLength;
    int nblocks;
    int vstride;
    int xoffset;
    vstride = x.size(0) - 1;
    bvstride = x.size(0) << 10;
    y.set_size(x.size(0));
    bsum.set_size(x.size(0));
    if (x.size(1) <= 1024) {
      firstBlockLength = x.size(1);
      lastBlockLength = 0;
      nblocks = 1;
    } else {
      firstBlockLength = 1024;
      nblocks = x.size(1) / 1024;
      lastBlockLength = x.size(1) - (nblocks << 10);
      if (lastBlockLength > 0) {
        nblocks++;
      } else {
        lastBlockLength = 1024;
      }
    }
    for (int xj{0}; xj <= vstride; xj++) {
      y[xj] = x[xj];
      bsum[xj] = 0.0;
    }
    for (int k{2}; k <= firstBlockLength; k++) {
      xoffset = (k - 1) * (vstride + 1);
      for (int xj{0}; xj <= vstride; xj++) {
        y[xj] = y[xj] + x[xoffset + xj];
      }
    }
    for (int ib{2}; ib <= nblocks; ib++) {
      xblockoffset = (ib - 1) * bvstride;
      for (int xj{0}; xj <= vstride; xj++) {
        bsum[xj] = x[xblockoffset + xj];
      }
      if (ib == nblocks) {
        firstBlockLength = lastBlockLength;
      } else {
        firstBlockLength = 1024;
      }
      for (int k{2}; k <= firstBlockLength; k++) {
        xoffset = xblockoffset + (k - 1) * (vstride + 1);
        for (int xj{0}; xj <= vstride; xj++) {
          bsum[xj] = bsum[xj] + x[xoffset + xj];
        }
      }
      for (int xj{0}; xj <= vstride; xj++) {
        y[xj] = y[xj] + bsum[xj];
      }
    }
  }
  firstBlockLength = y.size(0);
  for (xblockoffset = 0; xblockoffset < firstBlockLength; xblockoffset++) {
    y[xblockoffset] = y[xblockoffset] / static_cast<double>(x.size(1));
  }
}

} // namespace coder

//
// File trailer for mean.cpp
//
// [EOF]
//
